//
//  BK_Account_Recharge_VC.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/23.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_Account_Recharge_VC.h"
#import "BKODBRResultViewController.h"
#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"
#import "BK_Account_Recharge_Cell.h"
#import "BKOrderBRView.h"
#import "BKODAliManager.h"

NSString *const BKOrderBRRecharge = @"recharge/alipay";

@interface BK_Account_Recharge_VC ()
<
UITableViewDelegate,
UITableViewDataSource,
BKOrderBRFooterViewDelegate,
BKODAliManagerDelegate
>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *cellDataArr;
@property(nonatomic, strong)BKOrderBRFooterView *footerView;
@property(nonatomic, assign)NSInteger amountCellIndex;
@end

@implementation BK_Account_Recharge_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatCellData];
    [self initComponent];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)initComponent
{
    self.navigationItem.title = kOrderLocalizeString(@"BKOrderAccountRecharge");
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self makeConstraints];
    
    _footerView = [[BKOrderBRFooterView alloc] initWithFrame:CGRectMake(0, 0, kOrderScreenWidth, 48)];
    _footerView.delegate = self;
    _tableView.tableFooterView = _footerView;
}

- (void)makeConstraints
{
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)creatCellData
{
    CellModel *c0 = [[CellModel alloc] init];
    c0.className = NSStringFromClass([BKOrderBRAmountCell class]);
    c0.reuseIdentifier = @"BKOrderBRAmountCell";
    c0.height = [BKOrderBRAmountCell computeHeight:nil];
    
    CellModel *c1 = [[CellModel alloc] init];
    c1.className = NSStringFromClass([BKOrderBRPromptCell class]);
    c1.reuseIdentifier = @"BKOrderBRPromptCell";
    c1.height = [BKOrderBRPromptCell computeHeight:nil];
    
    CellModel *c2 = [[CellModel alloc] init];
    c2.className = NSStringFromClass([BKOrderBRMethodCell class]);
    c2.reuseIdentifier = @"BKOrderBRMethodCell";
    c2.height = [BKOrderBRMethodCell computeHeight:nil];
    
    SectionModel *s0 = [SectionModel sectionModelWithTitle:@"" cells:@[c0, c1, c2]];
    s0.headerhHeight = 10;
    s0.footerHeight = 60;
    _cellDataArr = @[s0];
    
    _amountCellIndex = [s0.cells indexOfObject:c0];
}

#pragma mark table  delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    SectionModel *sm = _cellDataArr[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    SectionModel *sm = _cellDataArr[section];
    return sm.footerHeight;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SectionModel *sm = _cellDataArr[section];
    return sm.cells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SectionModel *sm = _cellDataArr[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    BK_Account_Recharge_Cell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SectionModel *sm = _cellDataArr[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    return cm.height;
}

#pragma mark 确认充值
- (void)orderBRFooterViewDidSubmit
{
    [_tableView endEditing:YES];
    
    BKOrderBRAmountCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_amountCellIndex inSection:0]];
    NSString *amountText = cell.amountTextField.text;
    double amount = [amountText doubleValue];
    if (amount < 1) {
        kOrderPromptMsg(@"充值金额最少为1元");
        return;
    }
    long amount_int = amount;
    if (amount_int != amount) {
        kOrderPromptMsg(@"充值金额必须为1元的倍数");
        return;
    }
    if (amount > 99999999) {
        kOrderPromptMsg(@"充值金额需小于99999999元");
        return;
    }
    
    if ([RequestManager reachabilityStatus] == RequestReachabilityStatusUnknown || [RequestManager reachabilityStatus] == RequestReachabilityStatusNotReachable) {
        kOrderPromptMsg(@"请检查您的网络");
        return;
    }
    
    kOrderLoading;
    [BKODAliManager manager].delegate = self;
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(BKOrderBRRecharge) parameters:@{@"money" : amountText} success:^(id  _Nullable responseObject) {
        
        BOOL success = [responseObject[@"success"] boolValue];
        if (!success) {
            kOrderDismiss;
            kOrderPromptMsg(responseObject[@"msg"]);
            return;
        }
        
        kOrderDismiss;
        NSString *orderInfo = responseObject[@"data"][@"orderInfo"];
        [[BKODAliManager manager] payRequestOrderString:orderInfo];
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        kOrderDismiss;
        NSString *errorMsg;
        if (error.code == -1001) {
            errorMsg = @"请求超时";
        }
        else{
            errorMsg = @"请求失败";
        }
        kOrderPromptMsg(errorMsg);
    }];
    
}

//支付宝支付结果回调
- (void)ODWAliManagerDidRecvPayResult:(NSDictionary *)resultDic
{
    if ([resultDic[@"resultStatus"] isEqualToString:@"9000"]) {//支付成功
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(500 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
            BKODBRResultViewController *resultVC = [[BKODBRResultViewController alloc] init];
            [self.navigationController pushViewController:resultVC animated:YES];
        });
    }
    else{
        kOrderPromptMsg(@"支付失败");
    }
    
}


- (void)dealloc
{
    [BKODAliManager manager].delegate = nil;
}
@end




























